{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='https://raw.githubusercontent.com/autonomio/hyperio/master/logo.png' width=250px>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook is a compliment to the *Hyperparameter Optimization on Keras* article. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Overview\n",
    "\n",
    "There are four steps to setting up an experiment with Talos:\n",
    "\n",
    "1) Imports and data\n",
    "\n",
    "2) Creating the Keras model\n",
    "\n",
    "3) Defining the Parameter Space Boundaries \n",
    "\n",
    "4) Running the Experiment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. The Required Inputs and Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Dropout, Dense\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "import sys\n",
    "sys.path.insert(0, '/Users/mikko/Documents/GitHub/talos')\n",
    "import talos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# then we load the dataset\n",
    "x, y = talos.templates.datasets.breast_cancer()\n",
    "\n",
    "# and normalize every feature to mean 0, std 1\n",
    "x = talos.utils.rescale_meanzero(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Creating the Keras Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# first we have to make sure to input data and params into the function\n",
    "def breast_cancer_model(x_train, y_train, x_val, y_val, params):\n",
    "\n",
    "    model = Sequential()\n",
    "    model.add(Dense(params['first_neuron'], input_dim=x_train.shape[1],\n",
    "                    activation=params['activation'],\n",
    "                    kernel_initializer=params['kernel_initializer']))\n",
    "    \n",
    "    model.add(Dropout(params['dropout']))\n",
    "\n",
    "    model.add(Dense(1, activation=params['last_activation'],\n",
    "                    kernel_initializer=params['kernel_initializer']))\n",
    "    \n",
    "    model.compile(loss=params['losses'],\n",
    "                  optimizer=params['optimizer'],\n",
    "                  metrics=['acc', talos.utils.metrics.f1score])\n",
    "    \n",
    "    history = model.fit(x_train, y_train, \n",
    "                        validation_data=[x_val, y_val],\n",
    "                        batch_size=params['batch_size'],\n",
    "                        callbacks=[talos.utils.live()],\n",
    "                        epochs=params['epochs'],\n",
    "                        verbose=0)\n",
    "\n",
    "    return history, model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Defining the Parameter Space Boundary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# then we can go ahead and set the parameter space\n",
    "p = {'first_neuron':[9,10,11],\n",
    "     'hidden_layers':[0, 1, 2],\n",
    "     'batch_size': [30],\n",
    "     'epochs': [100],\n",
    "     'dropout': [0],\n",
    "     'kernel_initializer': ['uniform','normal'],\n",
    "     'optimizer': ['Nadam', 'Adam'],\n",
    "     'losses': ['binary_crossentropy'],\n",
    "     'activation':['relu', 'elu'],\n",
    "     'last_activation': ['sigmoid']}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Starting the Experiment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# and run the experiment\n",
    "t = talos.Scan(x=x,\n",
    "               y=y,\n",
    "               model=breast_cancer_model,\n",
    "               params=p,\n",
    "               experiment_name='breast_cancer',\n",
    "               round_limit=10)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
